﻿using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using LockCrypt.Core;

namespace LockCrypt.Mobile {
    public class WinMobileUtils {
        private static Dictionary<string, int> _iconMap;
        private static ImageList _templateImages;

        /// <summary>
        /// Gets the positions of icons in the image index (<see name="TemplateImages"/>)
        /// </summary>
        /// <value>The icon base-name&lt;-&gt;index mapping.</value>
        public static Dictionary<string, int> IconMap {
            get {
                if(_iconMap == null || _templateImages == null) {
                    BuildIconMap();
                }
                return _iconMap;
            }
        }

        /// <summary>
        /// Gets the cached template images from the icons directory.
        /// </summary>
        /// <value>The cached template images.</value>
        public static ImageList TemplateImages {
            get {
                if(_iconMap == null || _templateImages == null) {
                    BuildIconMap();
                }
                return _templateImages;
            }
        }

        /// <summary>
        /// Populates an imagelist used to display icons for accounts and groups and an 
        /// accompanying Dictionary indexing their positions in the list.
        /// </summary>
        public static void BuildIconMap() {
            _iconMap = new Dictionary<string, int>();
            _templateImages = new ImageList();
            _templateImages.Images.Add((Image)Properties.Resources.ResourceManager.GetObject("folder_open_16"));
            _iconMap.Add("folder_open_16", 0);
            _templateImages.Images.Add((Image)Properties.Resources.ResourceManager.GetObject("folder_closed_16"));
            _iconMap.Add("folder_closed_16", 1);
            Dictionary<string, string> icons = LockCryptBLL.GetIconNames(16);
            int i = 0;
            foreach(KeyValuePair<string,string> icon in icons) { // key = baseName, value = filename
                try {
                    Bitmap bmp = new Bitmap(icon.Value);
                    _templateImages.Images.Add(bmp);
                    bmp.Dispose();
                    _iconMap.Add(icon.Key, i + 2);
                    i++;
                } catch(Exception ex) {
                    Console.WriteLine("Error loading icon: " + ex.Message);
                }
            }
        }

        /// <summary>
        /// Gets the position of an image in the index.
        /// </summary>
        /// <param name="imageName">Name of the image.</param>
        /// <returns>The position of the icon, or the default icon if the image does not exist. Returns -1 if the image map contains neither.</returns>
        public static int GetImagePosition(string imageName) {
            int pos;
            if(!_iconMap.TryGetValue(imageName, out pos) && !_iconMap.TryGetValue(Constants.DefaultIcon, out pos)) {
                pos = -1;
            }
            return pos;
        }

        /// <summary>
        /// Tries to load an icon from the /Icons directory using the naming convention 'filename_size.png'. If the file does not exist,
        /// falls back to 'filename.png'. If both files are missing, uses the default icon.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="requiredSize">The required size.</param>
        /// <returns>An image of the specified size, or the default icon.</returns>
        public static Image GetSizedImage(string filename, int requiredSize) {
            string appDir = Lime49.Utils.GetApplicationPath();
            // try requested size
            string requestedImage = Path.Combine(appDir, string.Format("Icons{0}{1}_{2}.png", Path.DirectorySeparatorChar, filename, requiredSize));
            if(!File.Exists(requestedImage)) {
                // doesn't exist, try generic file
                requestedImage = Path.Combine(appDir, string.Format("Icons{0}{1}.png", Path.DirectorySeparatorChar, filename));
            }
            Image img;
            if(File.Exists(requestedImage)) {
                // also doesn't exist, use default icon
                img = new Bitmap(requestedImage);
            } else {
                img = (Image)Properties.Resources.ResourceManager.GetObject("default_" + requiredSize);
            }
            return img;
        }
    }
}
